package com.dqp.cslggroup.Calendar;

import android.content.Context;
import android.os.Build;

import com.dqp.cslggroup.SubjectView.MySubject;
import com.dqp.cslggroup.SubjectView.SubjectRepertory;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.GetTimeUtil;
import com.dqp.cslggroup.Util.StringUtil;
import com.dqp.cslggroup.Util.TimeTools;
import com.dqp.cslggroup.bean.CalendarEvent;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class CalendarUtil {
    private static int[] hour = new int[]{8, 9, 10, 11, 13, 14, 15, 16, 18, 19};
    private static int[] minute = new int[]{0, 0, 10, 10, 30, 30, 40, 40, 30, 30};

    static void setEvent(Context context, int alarmClock, int previousDate) {
        List<MySubject> mySubjectList = SubjectRepertory.loadSubjects();
        for (int i = 0; i < mySubjectList.size(); i++) {
            MySubject schedule = mySubjectList.get(i);
            String zcd = StringUtil.toZcd(schedule.getWeekList().toString());
            parsCalendarEventZcd(context, zcd, schedule, alarmClock, previousDate);
        }
    }

    private static void parsCalendarEventZcd(Context context, String zcd, MySubject schedule, int alarmClock, int previousDate) {
        CalendarEvent calendarEvent = new CalendarEvent();
        int year = GetTimeUtil.getYear();
        int month = GetTimeUtil.getMonth() - 1;
        int day = GetTimeUtil.getDay();
        int zhou = TimeTools.course_zhoushu(context);
        int toDayweek = TimeTools.week();
        int start = schedule.getStart();
        int step = schedule.getStep();
        int xqj = schedule.getDay() - 1;
        String rrule = null;
        if (zcd.contains("-") && zcd.contains(",")) {
            String[] arryZcd = zcd.split(",");
            for (String s : arryZcd)
                parsCalendarEventZcd(context, s, schedule, alarmClock, previousDate);
            return;
        } else if (zcd.contains(",") && !zcd.contains("-")) {
            String[] arryZcd = zcd.split(",");
            for (String s : arryZcd) {
                if (StringUtil.SelectNumber(s)[0] >= zhou) {
                    parsCalendarEventZcd(context, s, schedule, alarmClock, previousDate);
                }
            }
            return;
        } else if (zcd.contains("-") && !zcd.contains(",")) {
            if (zcd.contains("单")) {
                if (zhou % 2 != 0) {
                    if (xqj > toDayweek) {
                        int differ = xqj - toDayweek;
                        year = TimeTools.getDateAfterYear(new Date(), differ);
                        month = TimeTools.getDateAfterMonth(new Date(), differ);
                        day = TimeTools.getDateAfterDay(new Date(), differ);
                    } else {
                        int differ = toDayweek - xqj;
                        year = TimeTools.getDateBeforeYear(new Date(), differ);
                        month = TimeTools.getDateBeforeMonth(new Date(), differ);
                        day = TimeTools.getDateBeforeDay(new Date(), differ);
                    }
                    rrule = "FREQ=WEEKLY;INTERVAL=2;UNTIL=20200703T000000Z;";
                } else {
                    if (xqj > toDayweek) {
                        int differ = xqj - toDayweek + 7;
                        year = TimeTools.getDateAfterYear(new Date(), differ);
                        month = TimeTools.getDateAfterMonth(new Date(), differ);
                        day = TimeTools.getDateAfterDay(new Date(), differ);

                    } else {
                        int differ = toDayweek - xqj + 7;
                        year = TimeTools.getDateBeforeYear(new Date(), differ);
                        month = TimeTools.getDateBeforeMonth(new Date(), differ);
                        day = TimeTools.getDateBeforeDay(new Date(), differ);
                    }
                    rrule = "FREQ=WEEKLY;INTERVAL=2;UNTIL=20200703T000000Z;";
                }
            } else if (zcd.contains("双")) {
                if (zhou % 2 == 0) {
                    if (xqj > toDayweek) {
                        int differ = xqj - toDayweek;
                        year = TimeTools.getDateAfterYear(new Date(), differ);
                        month = TimeTools.getDateAfterMonth(new Date(), differ);
                        day = TimeTools.getDateAfterDay(new Date(), differ);

                    } else {
                        int differ = toDayweek - xqj;
                        year = TimeTools.getDateBeforeYear(new Date(), differ);
                        month = TimeTools.getDateBeforeMonth(new Date(), differ);
                        day = TimeTools.getDateBeforeDay(new Date(), differ);
                    }
                    rrule = "FREQ=WEEKLY;INTERVAL=2;UNTIL=20200703T000000Z;";
                } else {
                    if (xqj > toDayweek) {
                        int differ = xqj - toDayweek + 7;
                        year = TimeTools.getDateAfterYear(new Date(), differ);
                        month = TimeTools.getDateAfterMonth(new Date(), differ);
                        day = TimeTools.getDateAfterDay(new Date(), differ);

                    } else {
                        int differ = toDayweek - xqj + 7;
                        year = TimeTools.getDateBeforeYear(new Date(), differ);
                        month = TimeTools.getDateBeforeMonth(new Date(), differ);
                        day = TimeTools.getDateBeforeDay(new Date(), differ);
                    }
                    rrule = "FREQ=WEEKLY;INTERVAL=2;UNTIL=20200703T000000Z;";
                }
            } else {
                if (xqj > toDayweek) {
                    int differ = xqj - toDayweek;
                    year = TimeTools.getDateAfterYear(new Date(), differ);
                    month = TimeTools.getDateAfterMonth(new Date(), differ);
                    day = TimeTools.getDateAfterDay(new Date(), differ);
                } else {
                    int differ = toDayweek - xqj;
                    year = TimeTools.getDateBeforeYear(new Date(), differ);
                    month = TimeTools.getDateBeforeMonth(new Date(), differ);
                    day = TimeTools.getDateBeforeDay(new Date(), differ);
                }
                rrule = "FREQ=WEEKLY;INTERVAL=1;UNTIL=20200703T000000Z;";
            }
        } else if (!zcd.contains("-") && !zcd.contains(",")) {
            int[] zhous = StringUtil.SelectNumber(zcd);
            int differ = (zhous[0] - zhou) * 7;
            if (xqj > toDayweek) differ = differ + xqj - toDayweek;
            else differ = differ - (toDayweek - xqj);
            year = TimeTools.getDateAfterYear(new Date(), differ);
            month = TimeTools.getDateAfterMonth(new Date(), differ);
            day = TimeTools.getDateAfterDay(new Date(), differ);
            rrule = "FREQ=YEARLY;INTERVAL=1;UNTIL=20200703T000000Z;";
        }
        calendarEvent.setHour(hour[start - 1]);
        calendarEvent.setMinute(minute[start - 1]);
        calendarEvent.setSecond(0);
        calendarEvent.setAlarmClock(alarmClock);
        calendarEvent.setPreviousDate(previousDate);
        calendarEvent.setEnd(step);
        calendarEvent.setTitle(schedule.getName());
        calendarEvent.setDes("授课教师：" + schedule.getTeacher() + "\n"
                + "星期名称：" + TimeTools.toxinqi(start - 1) + "\n"
                + "上课节数：" + start + "-" + (start + step - 1) + "节" + "\n"
                + "上课周数：" + zcd + "\n"
                + "温馨提示：请认真核对相关课程日程的正确性！如果有误请及时修改或删除！\n"
                + "——" + year + "年" + month + "月" + day + "日" + "CSLG助手");
        calendarEvent.setLocation(schedule.getRoom());
        calendarEvent.setYear(year);
        calendarEvent.setMonth(month);
        calendarEvent.setDay(day);
        calendarEvent.setRrule(rrule);
        if (addMyCalendarEvent(context, calendarEvent))
            MyApplication.getDaoInstant().getCalendarEventDao().insert(calendarEvent);
    }

    private static boolean addMyCalendarEvent(Context context, CalendarEvent calendarEvent) {
        boolean succes = false;
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(System.currentTimeMillis());
        mCalendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        mCalendar.set(Calendar.YEAR, calendarEvent.getYear());
        mCalendar.set(Calendar.MONTH, calendarEvent.getMonth());
        mCalendar.set(Calendar.DAY_OF_MONTH, calendarEvent.getDay());
        mCalendar.set(Calendar.HOUR_OF_DAY, calendarEvent.getHour());      //时
        mCalendar.set(Calendar.MINUTE, calendarEvent.getMinute());         //分
        mCalendar.set(Calendar.SECOND, calendarEvent.getSecond());         //秒
        mCalendar.set(Calendar.MILLISECOND, 0);                            //分秒
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            succes = CalendarReminderUtils.addCalendarEvent(context, mCalendar, calendarEvent);
        }
        return succes;
    }

    public static void deleteAllMyCalendarEvent(Context context) {
        List<CalendarEvent> calendarEventList = MyApplication.getDaoInstant().getCalendarEventDao().loadAll();
        for (int i = 0; i < calendarEventList.size(); i++) {
            CalendarEvent calendarEvent = calendarEventList.get(i);
            deleteMyCalendarEvent(context, calendarEvent);
        }
    }

    private static boolean deleteMyCalendarEvent(Context context, CalendarEvent calendarEvent) {
        boolean succes = false;
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(System.currentTimeMillis());
        mCalendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        mCalendar.set(Calendar.YEAR, calendarEvent.getYear());
        mCalendar.set(Calendar.MONTH, calendarEvent.getMonth());
        mCalendar.set(Calendar.DAY_OF_MONTH, calendarEvent.getDay());
        mCalendar.set(Calendar.HOUR_OF_DAY, calendarEvent.getHour());      //时
        mCalendar.set(Calendar.MINUTE, calendarEvent.getMinute());         //分
        mCalendar.set(Calendar.SECOND, calendarEvent.getSecond());         //秒
        mCalendar.set(Calendar.MILLISECOND, 0);         //分秒
        long selectTime = mCalendar.getTimeInMillis();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            succes = CalendarReminderUtils.deleteCalendarEvent(context, calendarEvent.getTitle(), selectTime, calendarEvent.getTitle());
        }
        return succes;
    }


}
